package com.ocom.payconfig.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ocom.common.entity.card.EntityXfGoodsListProcess;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.CommonUtil;
import com.ocom.common.vo.payconfig.MealDateSummaryVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import com.ocom.payconfig.dao.IDaoXfGoodsListProcess;
import com.ocom.payconfig.service.XfGoodsListProcessService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service
public class XfGoodsListProcessServiceImpl extends ServiceImpl<IDaoXfGoodsListProcess, EntityXfGoodsListProcess> implements XfGoodsListProcessService {


    @Autowired
    IDaoXfGoodsListProcess iDaoXfGoodsListProcess;


    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean addXfGoodsListProcessList(Long companyId, List<EntityXfGoodsListProcess> list) {
        return  this.saveBatch(list);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfGoodsListProcess> getXfGoodsList(Long companyId,String orderNo) {
        if(CommonUtil.isNull(orderNo)) return  new ArrayList<>();
        QueryWrapper<EntityXfGoodsListProcess> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("order_no",orderNo);
        return  this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.SLAVE)
    public List<EntityXfGoodsListProcess> getXfGoodsList(Long companyId, EntityXfGoodsListProcess entityXfGoodsListProcess, Date beginTime, Date endTime) {
//        if(CommonUtil.isNull(orderNo)) return  new ArrayList<>();
        QueryWrapper<EntityXfGoodsListProcess> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("if_refund",0);
        queryWrapper.ne("receipt_id","1");
        if(ObjectUtil.isNotEmpty(beginTime)){
            queryWrapper.ge("create_time",beginTime);
        }

        if(ObjectUtil.isNotEmpty(endTime)){
            queryWrapper.le("create_time",endTime);
        }
//        queryWrapper.eq("order_no",orderNo);
        return  this.list(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean refund(Long companyId, String orderNo) {
        UpdateWrapper<EntityXfGoodsListProcess> queryWrapper =new UpdateWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("order_no",orderNo);
        queryWrapper.set("if_refund",1);
        return  this.update(queryWrapper);
    }

    @Override
    @DataSourceAspect(type= DataSourceEnum.PART_DB,isMaster = MasterSlaveEnum.MASTER)
    public Boolean refund(Long companyId, String orderNo,List<Integer> dishIds) {
        UpdateWrapper<EntityXfGoodsListProcess> queryWrapper =new UpdateWrapper<>();
        queryWrapper.eq("com_id",companyId);
        queryWrapper.eq("order_no",orderNo);
        if(ObjectUtil.isNotEmpty(dishIds)){
            queryWrapper.in("dishes_id",dishIds);
        }
        queryWrapper.set("if_refund",1);
        return  this.update(queryWrapper);
    }


    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER)
    public List<MealDateSummaryVo> mealDateSummary(Long companyId,
                                                   Integer pointId,
                                                   List<Integer> mealNos,
                                                   String beginDate,
                                                   String endDate) {
        return  iDaoXfGoodsListProcess.mealDateSummary(companyId,pointId,mealNos,beginDate,endDate);
    }

    @Override
    @DataSourceAspect(type = DataSourceEnum.PART_DB, isMaster = MasterSlaveEnum.MASTER,companyId = "comId")
    public Boolean updateById(Long comId, EntityXfGoodsListProcess entityXfGoodsListProcess) {
        return this.updateById(entityXfGoodsListProcess);
    }
}
